import {Injectable} from "@angular/core";
import {Device} from "@ionic-native/device";
import {AppHttp} from "../util/http";
import {services} from "./service";
import {interfaceUrls} from "./serverUrls";


@Injectable()
export class UserService{
    userId: any;
    userName: string;
    constructor(private http: AppHttp,
        private service: services,
        private device: Device){}

    login(param) {
        let p = {
            "username": param.username,
            "password": param.password,
            "appLocalId": this.device.uuid
        };
        console.info("登陆的请求数据",p);
        return this.http.post(interfaceUrls.login, p)
            .then(data => {
                this.userId = data.infoData.id;
                this.userName = data.infoData.loginName;
                this.service.infoData.id = data.infoData.id;
                this.service.infoData.loginName = data.infoData.loginName;
                localStorage.setItem("user",JSON.stringify(p));
                return  Promise.resolve(data);
            })
            .catch(err => {
                return Promise.reject(err);
            });
    }

    getLocalId(param): Promise<any> {
        return this.http.post(interfaceUrls.getLocalId,param);
    }

    //获取登录用户详细信息
    getPersonInfo(): Promise<any> {
        return this.http.post(interfaceUrls.personInfo,{});
    }

    //修改子账号名称
    editChildAccountName(param) {
        return this.http.post(interfaceUrls.editChildAccountName, param);
    }

    //修改个人资料
    editPersonalData(param) {
        return this.http.post(interfaceUrls.edit, param);
    }

    //登出
    logout(): Promise<any> {
        return this.http.post(interfaceUrls.logout, {});
    }

    //修改密码--发送短信验证码
    editPasswordCode() {
        return this.http.post(interfaceUrls.editPasswordCode, {});
    }

    //修改密码--修改密码
    editPassword(param) {
        return this.http.post(interfaceUrls.editPassword, param);
    }

    //找回密码--发送短信验证码
    findPasswordCode(param) {
        return this.http.post(interfaceUrls.findPasswordCode, param);
    }

    //找回密码--修改密码
    findPassword(param) {
        return this.http.post(interfaceUrls.findPassword, param);
    }
}